package com.gsafety.wellsfort.controller.organization;

import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.domain.dto.ids.AdminIdDTO;
import com.gsafety.wellsfort.domain.dto.secuirty.AdminDto;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.vo.AdminQueryVO;
import com.gsafety.wellsfort.domain.vo.TeacherVO;
import com.gsafety.wellsfort.service.security.IAdminService;
import com.gsafety.wellsfort.service.security.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
@Api(value = "教师专家管理", description = "教师专家管理接口")
@RequestMapping("/api/teacher")
public class TeacherController {

    private final static List<Integer> TYPES = Arrays.asList(new Integer[]{2, 3});
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;

    /**
     * 菜单列表
     *
     * @return
     */
    @ApiOperation(value = "讲师专家列表")
    @PostMapping("/list")
    public ApiResponse<List<Admin>> list(@RequestBody AdminQueryVO query) {
        if (query.getType() == null || query.getType() == 1) {
            return ApiResponse.error(ResultCode.NOT_AUTHORIZED);
        }
        Admin admin = LoginContextUtil.getAdmin();
        if (admin == null) {
            return ApiResponse.error(ResultCode.NOT_AUTHORIZED);
        }
        if (admin.getSchoolId() != 1) {
            query.setSchoolId(admin.getSchoolId());
        }
        return adminService.pageQuery(query);
    }


    @ApiOperation(value = "课程的讲师专家列表")
    @PostMapping("/getCourseTeacherList")
    public ApiResponse<List<Admin>> getCourseTeacherList() {
        AdminQueryVO adminQueryVO = new AdminQueryVO();
        Admin admin = LoginContextUtil.getAdmin();
        if (admin == null) {
            return ApiResponse.error(ResultCode.NOT_AUTHORIZED);
        }
        if (admin.getSchoolId() != 1) {
            //非平台
            if (admin.getType() == 1) {
                adminQueryVO.setSchoolId(admin.getSchoolId());
            } else {
                List<Admin> list = new ArrayList<>();
                list.add(admin);
                return ApiResponse.success(list);
            }
        }
        adminQueryVO.setTypes(TYPES);
        return adminService.pageQuery(adminQueryVO);
    }

    @ApiOperation(value = "添加教师、专家")
    @PostMapping(value = "/add")
    public ApiResponse add(@RequestBody TeacherVO teacherVO) {
        return adminService.add(convertTeacherVO2AdminDto(teacherVO));
    }

    @ApiOperation(value = "更新管理员")
    @PostMapping(value = "/update")
    public ApiResponse update(@RequestBody TeacherVO teacherVO) {
        if (adminService.update(convertTeacherVO2AdminDto(teacherVO))) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    @ApiOperation(value = "删除教师、管理员")
    @PostMapping(value = "/delete")
    public ApiResponse delete(@RequestBody AdminIdDTO idDTO) {
        if (adminService.delete(idDTO.getAdminId())) {
            return ApiResponse.success();
        } else {
            return ApiResponse.error();
        }
    }

    /**
     * teacherVO 转 adminVO
     *
     * @param teacherVO
     * @return
     */
    private AdminDto convertTeacherVO2AdminDto(TeacherVO teacherVO) {
        AdminDto adminDto = new AdminDto();
        BeanUtils.copyProperties(teacherVO, adminDto);
        return adminDto;
    }
}
